﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbConnect.Sql;
using DbConnect;

namespace DnDEngine.Class
{
    public class Download
    {

        #region "Private attributes"

        #endregion

        #region "Properties"

        public int ID
        {
            get;
            set;
        }

        public String Name
        {
            get;
            set;
        }

        public String  Path
        {
            get;
            set;
        }

        public String Date
        {
            get;
            set;
        }

        public Status Status
        {
            get;
            set;
        }


        #endregion

        #region "Events"

        #endregion

        #region "Public functions"

        public Download()
        {

            try
            {
                Logger.Log.TraceAction("Download.Constructor", "");

            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Download.Constructor", ex);
            }
            finally
            {
            }
        }

        public Download(int IDDownLoad)
        {

            try
            {
                Logger.Log.TraceAction("Download.Constructor", "");
                ID = IDDownLoad;
                Load();
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Download.Constructor", ex);
            }
            finally
            {
            }
        }


        #endregion

        #region "Private functions"

        private void Load()
        {


            try
            {
                Logger.Log.TraceAction("Download.Load", "");
                var data = new DbConnect.DataAccess();
                var req = SqlDownload.Select;
                data.AddParam(ParamDownload.ID, ID);

                var table = data.SelectWithParam(req);

                if (table != null && table.Rows.Count > 0)
                {
                    Name = table.Rows[0].GetStringValue("dName");
                    Path = table.Rows[0].GetStringValue("Path");
                    Date = table.Rows[0].GetStringValue("dDate");
                    Status = new Status(table.Rows[0].GetIntValue("Status"));
                }
                
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Download.Load", ex);
  
            }
            finally
            {
            }
        }

        private void Insert()
        {

            try
            {
                Logger.Log.TraceAction("Download.Insert", "");
                var data = new DbConnect.DataAccess();
                data.AddParam(ParamDownload.Name, Name);
                data.AddParam(ParamDownload.Path, Path);
                data.AddParam(ParamDownload.Date, Date);
                data.AddParam(ParamDownload.Status, Status.ID);

                var req = SqlDownload.InsertInto;

                data.Insert(req);

                ID = data.GetLastIDInserted(SqlDownload.TableName);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Download.Insert", ex);
            }
            finally
            {
            }
         }

        private void Update()
        {

            try
            {
                Logger.Log.TraceAction("Download.Update", "");
                var data = new DbConnect.DataAccess();
                data.AddParam(ParamDownload.Name, Name);
                data.AddParam(ParamDownload.Path, Path);
                data.AddParam(ParamDownload.Date, Date);
                data.AddParam(ParamDownload.ID, ID);
                data.AddParam(ParamDownload.Status, Status.ID);

                var req = SqlDownload.Update;

                data.Update(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Download.Update", ex);
            }
            finally
            {
            }
        }

        private void Delete()
        {

            try
            {
                Logger.Log.TraceAction("Download.Delete", "");
                var data = new DbConnect.DataAccess();

                data.AddParam(ParamDownload.ID, ID);

                var req = SqlDownload.Delete;

                data.Delete(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Download.Delete", ex);
            }
            finally
            {
            }
        }

        #endregion
		 
		 
    }
}
